package com.cn.ant.modules.school.web;

import com.cn.ant.common.config.Global;
import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.utils.AjaxJson;
import com.cn.ant.common.utils.StringUtils;
import com.cn.ant.common.web.BaseController;
import com.cn.ant.modules.school.entity.EnrollmentPlan;
import com.cn.ant.modules.school.entity.School;
import com.cn.ant.modules.school.entity.SchoolMajor;
import com.cn.ant.modules.school.service.EnrollmentPlanService;
import com.cn.ant.modules.school.service.SchoolMajorService;
import com.cn.ant.modules.school.service.SchoolService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 招生计划Controller
 * @author AntDream
 * @version 2015-08-29
 */
@Controller
@RequestMapping(value = "${adminPath}/school/enrollmentPlan")
public class EnrollmentPlanController extends BaseController {

	@Autowired
	private EnrollmentPlanService enrollmentPlanService;
	@Autowired
	private SchoolService schoolService;
	@Autowired
	private SchoolMajorService schoolMajorService;
	
	@ModelAttribute
	public EnrollmentPlan get(@RequestParam(required=false) String id) {
		EnrollmentPlan entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = enrollmentPlanService.get(id);
		}
		if (entity == null){
			entity = new EnrollmentPlan();
		}
		return entity;
	}
	
	@RequiresPermissions("school:enrollmentPlan:view")
	@RequestMapping(value = {"list", ""})
	public String list(EnrollmentPlan enrollmentPlan, HttpServletRequest request, HttpServletResponse response, Model model) {
		String schoolCode = request.getParameter("schoolCode");
		enrollmentPlan.setSchoolCode(schoolCode);
		Page<EnrollmentPlan> page = enrollmentPlanService.findPage(new Page<EnrollmentPlan>(request, response), enrollmentPlan);
		model.addAttribute("page", page);
		return "modules/school/enrollmentPlanList";
	}

	@RequiresPermissions("school:enrollmentPlan:view")
	@RequestMapping(value = "form")
	public String form(EnrollmentPlan enrollmentPlan,HttpServletRequest request, Model model) {
		String schoolCode = request.getParameter("schoolCode");
		String schoolCodeSn = request.getParameter("schoolCodeSn");
		if (StringUtils.isNotBlank(schoolCode)) {
			School school = schoolService.getByCode(schoolCode, schoolCodeSn);
			enrollmentPlan.setSchoolCode(school.getSchoolCode());
			enrollmentPlan.setSchoolName(school.getSchoolName());
		}
		//获取学校对应的专业
		List<SchoolMajor> schoolMajorList = schoolMajorService.queryBySchoolCode(schoolCode);
		model.addAttribute("schoolMajorList", schoolMajorList);
		model.addAttribute("enrollmentPlan", enrollmentPlan);
		return "modules/school/enrollmentPlanForm";
	}

	@RequiresPermissions("school:enrollmentPlan:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public AjaxJson save(EnrollmentPlan enrollmentPlan, HttpServletRequest request,Model model) {
		AjaxJson jo = new AjaxJson();
		try {
			if (!beanValidator(model, enrollmentPlan)) {
                jo.setSuccess(false);
                jo.setMsg("数据校验失败,请重试!");
                return jo;
			} else {
			    enrollmentPlanService.save(enrollmentPlan);
			}
			msg = "保存招生计划成功";
            jo.setMsg(msg);
            jo.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
            jo.setSuccess(false);
            jo.setMsg("操作失败,请联系管理员");
		}
		return jo;
	}
	
	@RequiresPermissions("school:enrollmentPlan:edit")
	@RequestMapping(value = "delete")
	public String delete(EnrollmentPlan enrollmentPlan, RedirectAttributes redirectAttributes) {
		enrollmentPlanService.delete(enrollmentPlan);
		addMessage(redirectAttributes, "删除招生计划成功");
		return "redirect:"+Global.getAdminPath()+"/school/enrollmentPlan/?repage";
	}

}